require('../css/practice.css');

// 课程训练页
document.ready(function () {

    const URL = 'http://139.9.177.51:8099';
    // 转换为json对象
    let user = JSON.parse(localStorage.getItem('user'));


    let oneImg = document.querySelector('.imgBox img');
    let textTop = document.querySelector('.textTop');
    let textBut = document.querySelector('.textBut');
    let newTiao = document.querySelector('#newTiao');


    // 最新课程
    http.get('/sports/courseList?id=' + user.userId, function (res) {
        console.log(res);
        let data = res.data;  //定义data
        //（最新课程latest=1） 返回数组当中满足条件的某个元素 用find()
        let test = res.data.find(function (item, index) {
            // 返回item下面的latest=1的数据 图片内容替换
            return item.latest === 1;
        })
        console.log(test);
        // 第一张图片 服务器地址加上图片地址
        oneImg.src = URL + test.imgurl;
        // 标题 内容的 替换重新渲染
        textTop.innerText = test.name;
        textBut.innerText = test.desc;
        newTiao.href = "./start.html?id=" + test.courseId;


        // forEach循环生成课程列表 
        let html = '';
        data.forEach(function (item, index) {
            // console.log(item);
            html += `
            <a href="./start.html?id=${item.courseId}">
              <div class="item">
                    <div class="item-img">
                        <img src="${URL}${item.imgurl}" alt="">
                    </div>
                    <div class="item-title">
                     ${item.name}
                    </div>
                    <div class="item-msg">
                     ${item.desc}
                    </div>
                </div>
            </a>

               
                `
        })
        document.querySelector('.data').innerHTML = html;






    })

















})